package base_NIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class WebClient {
    /*创建一个socket类型的通道s
    * 通道绑定远程ip和端口号
    *创建一个写模式的缓冲区w，
    * 对w写入数据后转为读模式的缓冲区
    * 通道s从w里读取数据到远程ip，发送数据。
    * */
    public static void main(String[] args) throws IOException {
        try {

            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("127.0.0.1", 8001));

            ByteBuffer writeBuffer = ByteBuffer.allocate(32);
            ByteBuffer readBuffer = ByteBuffer.allocate(32);

            writeBuffer.put("hello".getBytes());
            writeBuffer.flip();

//            while (true) {
//                writeBuffer.rewind();//
                socketChannel.write(writeBuffer);
//                readBuffer.clear();
//                socketChannel.read(readBuffer);
//            }
        } catch (IOException e) {
        }
    }
}